package secret.santa.client;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EventWindow extends Composite implements ClickHandler,
		KeyUpHandler 
{
	private TextBox eventNameTextBox;
	private TextBox eventDesctextBox;
	private Label eventNameLabel;
	private Label eventDescLabel;
	private FlexTable eventTable;
	
	private VerticalPanel eventPanel;
	
	private Button eventAddButton;
	private Button eventEditButton;
	private Button eventEditCancelButton;
	private Button addUserToEventButton;
	private Button closePopupButton;
	private Button generateListButton;
	private VerticalPanel evtEditPanel;
	private FlexTable evtTable;
	private NameSelector nameSelectBox;
	private FlexTable eventEntryTable;
	
	private PopupPanel glass;
    private PopupPanel popup;
	
	private Secret_Santa santa;
	
	private boolean eventEdit;
	private boolean eventDeleting;
	
	private CS_Event[] eventData;
	private IndexCheckBox[] userSelect;
	
	private int indexEventEdit;
	private String eventKey;
	
	public EventWindow(Secret_Santa s) 
	{
		santa = s;
		eventEdit = false;
		eventDeleting = false;
		indexEventEdit = -1;
		eventPanel = new VerticalPanel();
		initWidget(eventPanel);
		
		eventTable = new FlexTable();
		eventPanel.add(eventTable);
		eventTable.setBorderWidth(2);

		eventNameLabel = new Label("Event Name");
		eventTable.setWidget(0, 0, eventNameLabel);

		eventDescLabel = new Label("Description");
		eventTable.setWidget(0, 1, eventDescLabel);

		eventNameTextBox = new TextBox();
		eventNameTextBox.addKeyUpHandler(this);
		eventTable.setWidget(1, 0, eventNameTextBox);

		eventDesctextBox = new TextBox();
		eventDesctextBox.addKeyUpHandler(this);
		eventTable.setWidget(1, 1, eventDesctextBox);

		eventAddButton = new Button("Add Event");
		eventTable.setWidget(1, 2, eventAddButton);
		eventAddButton.addClickHandler(this);
		
		eventEditButton = new Button("Edit");
		eventEditButton.addClickHandler(this);
		
		eventEditCancelButton = new Button("Cancel");
		eventEditCancelButton.addClickHandler(this);
		createPopup();
		createEventEditPanel();
	}
	
	private void showPopup()
	{
	
		glass.show();
		popup.center();
	}
	public void eventDeleted()
	{
		eventDeleting = false;
	}
	public void eventEdited()
	{
		eventEdit = false;
		indexEventEdit = -1;
	}
	private void deleteEvent(int i) 
	{
		eventDeleting = true;
		santa.deleteEvent(eventData[i].encodedKey);
	}
	private void editEvent(int index) 
	{
		eventEdit = true;
		indexEventEdit = index -2;
		
		String name = eventData[indexEventEdit].name;
		String desc = eventData[indexEventEdit].description;
		eventTable.removeCells(index, 0, 4);
		eventTable.setWidget(index, 0, eventNameTextBox);
		eventTable.setWidget(index, 1, eventDesctextBox);

		eventTable.setWidget(index, 2, eventEditButton);
		eventTable.setWidget(index, 3, eventEditCancelButton);
		
		eventNameTextBox.setText(name);
		eventDesctextBox.setText(desc);
		
	}
	private void cancelEventEdit()
	{
		santa.getEvents();
		eventEdit = false;
		indexEventEdit = 0;
		
		eventTable.remove(eventEditButton);
		eventTable.remove(eventEditCancelButton);
		
		eventNameTextBox.setText("");
		eventDesctextBox.setText("");
		
	}
	
	private void createPopup() 
	{
		glass = new PopupPanel();
		glass.addStyleName("fadeEffect");

		popup = new PopupPanel(false);
		popup.addStyleName("popup");
		
		closePopupButton = new Button("Cancel");
		closePopupButton.addClickHandler(this);
		
	}
	private void createEventEditPanel() 
	{
		evtEditPanel = new VerticalPanel();
		popup.add(evtEditPanel);
		
		evtTable = new FlexTable();
		evtEditPanel.add(evtTable);
		evtTable.setBorderWidth(2);
		nameSelectBox = new NameSelector();
		
		eventEntryTable = new FlexTable();
		eventEntryTable.setBorderWidth(2);
		evtEditPanel.add(eventEntryTable);
		
		generateListButton = new Button("Calculate list");
		generateListButton.addClickHandler(this);
		
		addUserToEventButton = new Button("Add");
		addUserToEventButton.addClickHandler(this);

		evtEditPanel.add(nameSelectBox);
		evtEditPanel.add(addUserToEventButton);
		evtEditPanel.add(generateListButton);
		evtEditPanel.add(closePopupButton);
		evtTable.setWidget(0, 0, new Label("Participants"));
		
	}
	@Override
	public void onClick(ClickEvent event)
	{
		if(event.getSource() == eventAddButton)
			addEvent();
		else if(event.getSource().getClass() == DeleteButton.class)
			deleteEvent(((DeleteButton)event.getSource()).getIndex());
		else if(event.getSource().getClass() == EditButton.class)
			editEvent(((EditButton)event.getSource()).getIndex());
		else if(event.getSource()== eventEditButton)
			editEvent();
		else if(event.getSource() == eventEditCancelButton)
			cancelEventEdit();
		else if(event.getSource() == addUserToEventButton)
			addUserToEvent();
		else if(event.getSource() == closePopupButton)
			closePopup();
		else if(event.getSource()== generateListButton)
			generateList();
		else if(event.getSource().getClass() == IndexAnchor.class)
			queryForEvent(((IndexAnchor)event.getSource()).getIndex());
		
	}
	private void closePopup() 
	{
		popup.hide();
		glass.hide();
		
	}
	private void addUserToEvent() 
	{
		santa.addUsersToEvent(nameSelectBox.getSelectedItems(), eventKey);
		
	}
	private void generateList()
	{
		santa.generateList(eventKey);
		
	}

	@Override
	public void onKeyUp(KeyUpEvent event) 
	{
		if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) 
		{
			if(event.getSource() == eventNameTextBox || event.getSource() == eventDesctextBox)
			{
				if(eventEdit)
					editEvent();
				else
					addEvent();
			}
		}

	}
	private void editEvent() 
	{
		String name = eventNameTextBox.getText();
		String desc = eventDesctextBox.getText();
	
		if(name.length() == 0 || name.length() == 0)
			Window.alert("One of the required fields is empty.");
		else
			santa.editEvent(eventData[indexEventEdit].encodedKey, name, desc);
		eventNameTextBox.setText("");
		eventDesctextBox.setText("");
		
	}
	private void queryForEvent(int index) 
	{
		santa.queryForEvent(eventData[index].encodedKey);
		eventKey = eventData[index].encodedKey;
		
	}
	private void addEvent()
	{
		String name = eventNameTextBox.getText();
		String desc = eventDesctextBox.getText();
	
		if(name.length() == 0 || name.length() == 0)
			Window.alert("One of the required fields is empty.");
		else
			santa.addEvent(name, desc);
		eventNameTextBox.setText("");
		eventDesctextBox.setText("");
	}
	public void displayEvents(CS_Event[] e) 
	{
		int size = e.length;
		
		eventData = e;
		
		eventTable.clear();
		
		initEventHeader();
		for(int i = 0; i < size; i++)
		{
			DeleteButton x = new DeleteButton();
			
			IndexAnchor a = new IndexAnchor();
			a.setText(e[i].name);
			a.setIndex(i);
			a.addClickHandler(this);
	
			x.addClickHandler(this);
			x.setIndex(i);
			EditButton edit = new EditButton();
			edit.setIndex(i);
			edit.addClickHandler(this);
			eventTable.setWidget(i+2,0,a);
			eventTable.setWidget(i+2,1,new Label(e[i].description));
			eventTable.setWidget(i+2, 2, x);
			eventTable.setWidget(i+2, 3, edit);
			
		}
		
	}
	private void initEventHeader() 
	{
		eventTable.setWidget(0, 0, eventNameLabel);
		eventTable.setWidget(0, 1, eventDescLabel);
		eventTable.setWidget(1, 0, eventNameTextBox);
		eventTable.setWidget(1, 1, eventDesctextBox);

		eventTable.setWidget(1, 2, eventAddButton);
		
	}
	public void displayEventQuery(EventQuery result) 
	{	
		//TODO: remove users that are already in the event.
		
		evtTable.clear();
		if(result.users != null)
		{
			int size = result.users.length;
			for(int i = 0; i < size; i++ )
			{
				evtTable.setWidget(i+1, 0, new Label(result.users[i].fullName));
			}
		}
		
		eventEntryTable.clear();
		if(result.entries != null)
		{
			eventEntryTable.clear();
			int size = result.entries.length;
			for(int i = 0; i < size; i++)
			{
				eventEntryTable.setWidget(i, 0, new Label(result.entries[i].from));
				eventEntryTable.setWidget(i, 1, new Label("=>"));
				eventEntryTable.setWidget(i, 2, new Label(result.entries[i].to));
				
			}
		}
		CS_User[] userData = santa.getUserData();
		int size = userData.length;
		nameSelectBox.clear();
		for(int i = 0; i < size; i++)
		{
			nameSelectBox.addItem(userData[i].fullName, userData[i].encodedKey);
		}
		
		
		showPopup();
	}
	public void queryForEvent() 
	{
		santa.queryForEvent(eventKey);
	}
}
